package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Permission;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    public static final String SUCCESS_PAGE = "common/successPage";
    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;

    //    @RequestMapping
//    public String index(Map map){
//        List<Role> roleList = roleService.findAll();
//        map.put("list",roleList);
//        return "role/index";
//    }
    @PreAuthorize("hasAnyAuthority('role.show')")
    @RequestMapping
    public String string(Map map, HttpServletRequest request) {
//           获取请求参数
        Map<String, Object> filters = getFilters(request);
//            filters放到request域中
        map.put("filters", filters);
//            调用RoleService中分页及带条件查询的方法
        PageInfo<Role> pageInfo = roleService.findPage(filters);
        map.put("page", pageInfo);
        return "role/index";
    }

    //    去添加角色页面
    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/create")
    public String goAddPage() {
        return "role/create";
    }

    //    添加角色
    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/save")
    public String save(Role role) {
        roleService.insert(role);
//        重定向到查询所有角色的方法，不友好
//        return "redirect:/role";
        return SUCCESS_PAGE;
    }

    //    删除角色
    @PreAuthorize("hasAnyAuthority('role.delete')")
    @RequestMapping("/delete/{roleId}")
    public String delete(@PathVariable("roleId") Long id) {
        roleService.delete(id);
        // 重定向到查询所有角色的方法
        return "redirect:/role";
    }

    //    去编辑页面
    @PreAuthorize("hasAnyAuthority('role.edit')")
    @RequestMapping("/edit/{roleId}")
    public String toEditPage(@PathVariable("roleId") Long id, Map map) {
        Role role = roleService.getById(id);
        map.put("role", role);
        return "role/edit";
    }

    //    编辑
    @PreAuthorize("hasAnyAuthority('role.edit')")
    @RequestMapping("/update")
    public String update(Role role) {
        roleService.update(role);
        return SUCCESS_PAGE;
    }

    //    去分配权限的页面
    @PreAuthorize("hasAnyAuthority('role.assgin')")
    @RequestMapping("/assignShow/{roleId}")
    public String goAssignShowPage(@PathVariable("roleId") Long roleId, Map map) {
        map.put("roleId", roleId);
//            根据角色id获取权限的方法
        List<Map<String, Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        map.put("zNodes", zNodes);
        return "role/assignShow";
    }

    //    分配权限
    @PreAuthorize("hasAnyAuthority('role.assgin')")
    @RequestMapping("/assignPermission")
    public String assignPermission(@RequestParam("roleId") Long roleId, @RequestParam("permissionIds") Long[] permissionIds) {
        permissionService.assignPermission(roleId, permissionIds);
        return "common/successPage";
    }
}
